<template>
  <div>
    <el-row>
      <el-button v-if="hasPermission('lora:area:jfLoraGsArea:add')" type="primary" size="small" icon="el-icon-plus"
        @click="add()">新建</el-button>
    </el-row>
    <el-table :data="dataList" size="medium" row-key="id" v-loading="loading" class="table">
      <el-table-column prop="name" show-overflow-tooltip label="名称">
      </el-table-column>
      <el-table-column prop="areaType" show-overflow-tooltip label="区域类型">
        <template slot-scope="scope">
          {{ $dictUtils.getDictLabel("lora_fence_area", scope.row.areaType, '-') }}
        </template>
      </el-table-column>
      <!-- <el-table-column
        prop="areaInfo"
        show-overflow-tooltip
        label="区域信息">
      </el-table-column> -->
      <!-- <el-table-column prop="gaiyaSystemId" show-overflow-tooltip label="盖雅系统ID">
      </el-table-column> -->
      <el-table-column header-align="center" align="center" fixed="right" width="300" label="操作">
        <template slot-scope="scope">
          <el-button v-if="hasPermission('lora:area:jfLoraGsArea:view')" type="text" size="small" icon="el-icon-view"
            @click="view(scope.row.id)">查看</el-button>
          <el-button v-if="hasPermission('lora:area:jfLoraGsArea:edit')" type="text" size="small" icon="el-icon-edit"
            @click="edit(scope.row.id)">修改</el-button>
          <el-button v-if="hasPermission('lora:area:jfLoraGsArea:del')" type="text" size="small" icon="el-icon-delete"
            @click="del(scope.row.id)">删除</el-button>
          <el-button v-if="hasPermission('lora:area:jfLoraGsArea:add') && scope.row.parentIds ==='0,'" type="text" size="small"
            icon="el-icon-circle-plus-outline" @click="addChild(scope.row.id, scope.row.name)">添加区域</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗, 新增 / 修改 -->
    <JfLoraGsAreaForm ref="jfLoraGsAreaForm" @refreshDataList="refreshList"></JfLoraGsAreaForm>
  </div>
</template>

<script>
  import TableTreeColumn from '@/components/table-tree-column'
  import JfLoraGsAreaForm from './JfLoraGsAreaForm'
  export default {
    data() {
      return {
        dataList: [],
        loading: false
      }
    },
    components: {
      TableTreeColumn,
      JfLoraGsAreaForm
    },
    activated() {
      this.refreshList()
    },

    methods: {
      // 获取数据列表
      refreshList() {
        this.loading = true
        this.$http({
          url: '/lora/area/jfLoraGsArea/list',
          method: 'get'
        }).then(({
          data
        }) => {
          this.dataList = this.treeDataTranslate(data.list, 'id', 'parentId', 'children')
          this.loading = false
        })
      },
      // 新增下级
      addChild(id, name) {
        this.$refs.jfLoraGsAreaForm.init('addChild', {
          id: '',
          parent: {
            id: id,
            name: name
          }
        })
      },
      // 新增
      add() {
        this.$refs.jfLoraGsAreaForm.init('add', {
          id: '',
          parent: {
            id: '',
            name: ''
          }
        })
      },
      // 修改
      edit(id) {
        this.$refs.jfLoraGsAreaForm.init('edit', {
          id: id,
          parent: {
            id: '',
            name: ''
          }
        })
      },
      // 查看
      view(id) {
        this.$refs.jfLoraGsAreaForm.init('view', {
          id: id,
          parent: {
            id: '',
            name: ''
          }
        })
      },
      // 删除
      del(id) {
        this.$confirm(`确定删除所选项吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.$http({
            url: '/lora/area/jfLoraGsArea/delete',
            method: 'delete',
            params: {
              'id': id
            }
          }).then(({
            data
          }) => {
            if (data && data.success) {
              this.$message.success(data.msg)
              this.refreshList()
            }
            this.loading = false
          })
        })
      }
    }
  }
</script>
